package org.phalanx.server.user;

import java.sql.SQLException;
import java.util.ArrayList;

import org.phalanx.server.PhalanxServer;
import org.phalanx.server.ServerCache.AbstractCacheHandler;
import org.phalanx.server.ServerCache.Cache;
import org.phalanx.server.ServerCache.Cacheable;
import org.phalanx.server.ServerCache.QueryObject;
import org.phalanx.server.sql.SQLAction;
import org.phalanx.server.sql.SQLManager;

public class UserCacheHandler implements AbstractCacheHandler {
	public static String TableName = "users";
	public static final String CACHENAME = "UserCache";
	private static String ID = "userid";
	private static String NAME = "name";
	private static String PASSWORD = "password";
	private static String LASTLOGIN = "modifiedtime";
	private static String CREATEDTIME = "createdtime";
	
	public UserCacheHandler(){

	}
	
	public boolean addElementToDB(Cache c, Object newKey, Cacheable newElement) {
		// TODO Auto-generated method stub
		boolean isSuccess = false;
		SQLManager sqlMan = PhalanxServer.getServerInstance().getSQLManager();
		String sqlString;
		
		SQLAction addUserToDB = new SQLAction(SQLAction.INSERT, TableName);

		try {
			if (newElement != null){
				PhalanxUser userInDB = (PhalanxUser)newElement;
				
				addUserToDB.addField(NAME);
				addUserToDB.addField(PASSWORD);
				addUserToDB.addField(CREATEDTIME);
				addUserToDB.addField(LASTLOGIN);
				
				addUserToDB.addValues(userInDB.getUserName());
				addUserToDB.addValues(userInDB.getPassword());
				addUserToDB.addValues(userInDB.getCreateDate().toString());
				addUserToDB.addValues(userInDB.getLastLogin().toString());
				
				sqlString = addUserToDB.getActionString();

				
				isSuccess = sqlMan.insertQuery(sqlString);

				/*if (getObjectFromDB(c, newKey, true) == null){
					isSuccess = false;
				}*/

			}
		} catch (SQLException se){
			se.printStackTrace();
		}

		return isSuccess;
	}

	public boolean deleteObjectFromDB(Cache c, Object newKey, Cacheable newElement) {
		// TODO Auto-generated method stub
		return true;
	}

	public ArrayList<Cacheable> getMultipleObjectsFromDB(Cache c, Object newKey,
			boolean cacheUpdate) {
		// TODO Auto-generated method stub
		return null;
	}

	public Cacheable getObjectFromDB(Cache c, Object newKey, boolean cacheUpdate) {
		SQLManager sqlMan = PhalanxServer.getServerInstance().getSQLManager();
		ArrayList<QueryObject> results = new ArrayList<QueryObject>();
		PhalanxUser newUserFromDB = null;

		QueryObject userQuery = new QueryObject();
		userQuery.setString(ID, new String());
		userQuery.setString(NAME, new String());
		userQuery.setString(PASSWORD, new String());
		userQuery.setString(CREATEDTIME, new String());
		userQuery.setString(LASTLOGIN, new String());
		
		SQLAction selectUser = new SQLAction(SQLAction.SELECT, TableName);
		selectUser.addField("*");
		selectUser.addSearchCriteriaAND(ID, SQLAction.EQUALS, newKey.toString());

		try {
			results = sqlMan.getQuery(selectUser.getActionString(), userQuery);
		} catch (SQLException se){
			se.printStackTrace();
		}

		if (results.size() > 0){
			QueryObject temp = results.get(0);

			newUserFromDB = new PhalanxUser();
			newUserFromDB.setId(temp.getInteger(ID));
			newUserFromDB.setUserName(temp.getString(NAME));
			newUserFromDB.setPassword(temp.getString(PASSWORD));
			newUserFromDB.setCreateDate(temp.getTimeStamp(CREATEDTIME).getTime());
			newUserFromDB.setLastLogin(temp.getTimeStamp(LASTLOGIN).getTime());

			//c.put(newUserFromDB.getLongName(), newUserFromDB);
		}


		return newUserFromDB;
	}

	public boolean populateCache(Cache c) {
		// TODO Auto-generated method stub
		// Temporary Measures until we get some JDBC persistence
		// We will manually populate the cache for now.
		boolean isSuccess = false;
		
		SQLManager sqlMan = PhalanxServer.getServerInstance().getSQLManager();
		ArrayList<QueryObject> results = new ArrayList<QueryObject>();
		
		QueryObject userQuery = new QueryObject();
		userQuery.setString(ID, new String());
		userQuery.setString(NAME, new String());
		userQuery.setString(PASSWORD, new String());
		userQuery.setString(CREATEDTIME, new String());
		userQuery.setString(LASTLOGIN, new String());
		
		SQLAction getAllUsers = new SQLAction(SQLAction.SELECT, TableName);
		getAllUsers.addField("*");

		try {
			results = sqlMan.getQuery(getAllUsers.getActionString(), userQuery);
		} catch (SQLException se){
			se.printStackTrace();
		}
		
		for (int i = 0; i < results.size(); i++){
			QueryObject temp = results.get(i);
			
			PhalanxUser newUserFromDB = new PhalanxUser();
			newUserFromDB.setId(temp.getInteger(ID));
			newUserFromDB.setUserName(temp.getString(NAME));
			newUserFromDB.setPassword(temp.getString(PASSWORD));
			newUserFromDB.setCreateDate(temp.getTimeStamp(CREATEDTIME).getTime());
			newUserFromDB.setLastLogin(temp.getTimeStamp(LASTLOGIN).getTime());
			
			c.put(newUserFromDB.getId(), newUserFromDB);
		}
		
		
		return isSuccess;
	}

	public boolean updateElementToDB(Cache c, Object newKey, Cacheable newElement) {
		// TODO Auto-generated method stub
		boolean isSuccess = false;
		SQLManager sqlMan = PhalanxServer.getServerInstance().getSQLManager();
		PhalanxUser updatedUser = (PhalanxUser)newElement;
		SQLAction updatingUser = new SQLAction(SQLAction.UPDATE, TableName);
		
		updatingUser.addField(NAME);
		updatingUser.addField(PASSWORD);
		updatingUser.addField(CREATEDTIME);
		updatingUser.addField(LASTLOGIN);
		
		updatingUser.addValues(updatedUser.getUserName());
		updatingUser.addValues(updatedUser.getPassword());
		updatingUser.addValues(updatedUser.getCreateDate().toString());
		updatingUser.addValues(updatedUser.getLastLogin().toString());
		
		updatingUser.addSearchCriteriaAND(ID, SQLAction.EQUALS, updatedUser.getId().toString());
		
		try {
			sqlMan.insertQuery(updatingUser.getActionString());
			isSuccess = true;
		} catch (SQLException se){
			se.printStackTrace();
		}
		
		
		return isSuccess;
	}

}
